package com.shundr.shipper.order.http;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.shundr.shipper.frame.constant.LCommands;
import com.shundr.shipper.frame.http.HttpResult;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.http.LHttpRequest.RequestCompleteListener;
import com.shundr.shipper.order.model.CargoOrderInfo;

public class OrderRequest extends LHttpRequest implements RequestCompleteListener{
	
	public static final int GET_ORDER_LIST_SUCCESSED = 1;//获取订单列表
	public static final int GET_ORDER_DETAILS_SUCCESSED = 2;//获取订单详情
	public static final int UPDATE_ORDER_SUCCESSED= 3;//获取订单详情
	public static final int SAVE_OEDER_SUCCESSED=4;//保存订单
	
	/**
	 * 为个人车辆
	 */
	public static final String INDIVIDUAL = "0";
	/**
	 * 为公司车辆
	 */
	public static final String COMPANY = "1";
	
	private Handler mHandler;
	private Message mMessage;
	private Context mContext;
	
	private Map<String, Object> param;
	private String command;
	
	public OrderRequest(Context context, Handler handler){
		this.mContext = context;
		this.mHandler = handler;
		this.mMessage = mHandler.obtainMessage();
		this.setRequestCompleteListener(this);
	}
	
	/**
	 * 
	 * getOrderLsit(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param pageSize
	 * @param accessToken
	 * @param orderStatus
	 * @param offset
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getOrderLsit(int pageSize,String orderStatus, int offset){
		param = new HashMap<String, Object>();
		param.put("pageSize", pageSize);
		param.put("orderStatus", orderStatus);
		param.put("offset", offset);
		command = LCommands.GET_ORDER_LIST;
		requestByGet(param);
	}
	
	/**
	 * 保存订单
	 * @param truckResourceType
	 * @param cargoUserId
	 * @param truckUserId
	 * @param truckId
	 * @param cargoId
	 */
	public void saveOrder(String truckResourceType,String cargoUserId,String truckUserId,String truckId,String cargoId) {
		param = new HashMap<String, Object>();
		param.put("truckResourceType", truckResourceType);
		param.put("cargoUserId", cargoUserId);
		param.put("truckUserId", truckUserId);
		param.put("truckId", truckId);
		param.put("cargoId", cargoId);
		command = LCommands.SAVE_OEDER;
		requestByPost(param);
	}
	
	/**
	 * 订单详情
	 * @param orderId
	 */
	public void getOrderDetailByOrderID(String orderId){
		param = new HashMap<String, Object>();
		param.put("orderId", orderId);
		command = LCommands.GET_ORDER_DETAILS;
		requestByGet(param);
	}
	
	/**
	 * 确认/拒绝订单
	 * @param orderId
	 */
	public void updateOrderStatus(String orderId,int status) {
		param = new HashMap<String, Object>();
		param.put("orderId", orderId);
		param.put("status", status);
		command = LCommands.UPDATE_ORDER_STATUS;
		requestByPost(param);
	}
	@Override
	protected void initParams() {
		super.initParams();
		mParam = param;
		mCommand = command;
		mBaseContext = mContext;
	}

	@Override
	public void requestSuccessed(HttpResult result) {
		if(mHandler == null)
			return;
		if (command.equals(LCommands.GET_ORDER_LIST)) {
			mMessage.what = GET_ORDER_LIST_SUCCESSED;
			List<CargoOrderInfo> infos = OrderParser.parserOrderList(result.getResult());
			mMessage.obj = infos;	
		}else if(command.equals(LCommands.GET_ORDER_DETAILS)){
			mMessage.what = GET_ORDER_DETAILS_SUCCESSED;
			CargoOrderInfo infos=OrderParser.parserOrderInfo(result.getResult());
			mMessage.obj = infos;	
		}else if(command.equals(LCommands.UPDATE_ORDER_STATUS)){			
			mMessage.what = UPDATE_ORDER_SUCCESSED;
			mMessage.obj = result.getDescription();	
		}else if(command.equals(LCommands.SAVE_OEDER)){
			mMessage.what = SAVE_OEDER_SUCCESSED;
			String orderId=result.getResult();
			mMessage.obj = orderId;	
		}
		mHandler.sendMessage(mMessage);
	}

	@Override
	public void requestFailed(HttpResult result) {
		if(mHandler == null)
			return;
		//请求失败,result的返回码可判断失败原因，进行如下操作
		mMessage.what = REQUEST_FAILED;
		mMessage.obj = result.getDescription();
		mHandler.sendMessage(mMessage);
	}

}
